package edu.hut.bookshop.service.impl;

import com.github.pagehelper.PageHelper;
import edu.hut.bookshop.dao.BookDisplayMapper;
import edu.hut.bookshop.dao.BookMapper;
import edu.hut.bookshop.dao.CategoryMapper;
import edu.hut.bookshop.pojo.Book;
import edu.hut.bookshop.pojo.Category;
import edu.hut.bookshop.service.BookDisplayService;
import edu.hut.bookshop.util.RedisUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 首页书籍展示模块实现类
 * @Author: guozongchao
 * @Date: 2020/6/4 13:37
 */
@Service
public class BookDisplayServiceImpl implements BookDisplayService {
    @Resource
    private BookMapper bookMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private BookDisplayMapper bookDisplayMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate redisTemplate1;


    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.selectAll();
    }

    @Override
    public List<Book> getAllBooks(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Book> books = bookMapper.selectAllByCategoryCode(null);
        return books;
    }

    @Override
    public List<Book> getBooksByCategoryCode(Integer page, Integer limit,String categoryCode) {
        PageHelper.startPage(page,limit);
        //先从Redis缓存中查询数据
        List<Book> bookList = new ArrayList<>();
        Set<String> keys = redisTemplate.keys("selectPageByCategoryCode_" + categoryCode);
        if(!keys.isEmpty()){
        List<List<Book>> values = redisTemplate.opsForValue().multiGet(keys);
        if(values.listIterator().hasNext()) {
             bookList = values.listIterator().next();
            System.out.println("va="+bookList);
        }
        ArrayList<String> list = new ArrayList<String>();
        System.out.println("找到了"+keys);
            System.out.println("booklist="+bookList);
            return  bookList;
        }else {
            //System.out.println("失败的缓存");
            //缓存中没有，就存入缓存
            List<Book> books = bookMapper.selectAllByCategoryCode(categoryCode);
            List<Book> book = new ArrayList<Book>();
            //redisUtil.hset("jiajus","jiajus",books);
            //进行缓存，时间为两分钟

            System.out.println(books.get(1));
            System.out.println(books.listIterator().next());
                redisTemplate.opsForValue().set("selectPageByCategoryCode_" + categoryCode, books, 1, TimeUnit.MINUTES);
            //System.out.println("查询结果为:"+redisUtil.hget("jiajus","jiajus"));
            return books;
        }
    }

    @Override
    public Book getBookDetailsByBookId(Integer bookId) {
        return bookMapper.selectByBookId(bookId);
    }

    @Override
    public List<Book> searchBooksByBookName(Integer page, Integer limit,String bookName) {
        PageHelper.startPage(page, limit);
        List<Book> books = bookDisplayMapper.fuzzyQueryByBookName(bookName);
        return books;
    }
}
